import LocalCache from '@/utils/cache'

export interface CacheResult {
    cachedQuery?: any; // 根据实际情况定义query的类型
    cachedColumns?: any[]; // 根据实际情况定义columns的类型
}
// 获取table查询缓存
// export const getQueryTableCache = (route: string, tableId: string, queryObj: Object, columnsObj: any[]): CacheResult => {
//     const queryKey = `userId-${route}-${tableId}`;
//     const columnsKey = `userId-${route}-${tableId}-columns`;

//     const cachedQuery = LocalCache.getCache(queryKey);
//     const cachedColumns = LocalCache.getCache(columnsKey);

//     return {
//         cachedQuery: cachedQuery,
//         cachedColumns: cachedColumns
//     };
// };
export const getQueryTableCache = (route: string, tableId: string, queryObj: Object, columnsObj: any[]): CacheResult => {
    const userData = LocalCache.getCache('user');
    const userId = userData.userId;
    console.log('userid------', userData, userId)
    const queryKey = `${userId}-${route}-${tableId}`;
    const columnsKey = `${userId}-${route}-${tableId}-columns`;

    const cachedQuery = LocalCache.getCache(queryKey);
    const cachedColumns = LocalCache.getCache(columnsKey);

    // 遍历queryObj，如果有缓存值则覆盖
    if (cachedQuery) {
        for (const key in queryObj) {
            if (cachedQuery.hasOwnProperty(key)) {
                queryObj[key] = cachedQuery[key];
            }
        }
    }

    // 对于columnsObj，简单按索引对应更新（需确保两数组长度一致且一一对应）
    if (cachedColumns && cachedColumns.length === columnsObj.length) {
        for (let i = 0; i < columnsObj.length; i++) {
            columnsObj[i] = cachedColumns[i];
            // 遍历列对象，将缓存的列对象中的值覆盖到当前列对象中
            for (const key in columnsObj[i]) {
                if (cachedColumns[i].hasOwnProperty(key)) {
                    columnsObj[i][key] = cachedColumns[i][key];
                }
            }
        }
    }

    return {
        cachedQuery: queryObj,
        cachedColumns: columnsObj
    };
};